Ubuntu 开机自动执行 | 您所在的位置:网站首页 › ubuntu 执行shell脚本 › Ubuntu 开机自动执行 |
背景
开发过程中,有时需要一些sh脚本、python脚本、命令等在开机的时候自动执行;这种方法比较适合于ubuntu16及之前的版本;毕竟像ubunutu18本来是不带rc.local脚本的。 简介rc.local脚本是一个Ubuntu开机后会自动执行的脚本,在该脚本内添加命令行,开机时会自动执行。 脚本路径:/etc/rc.local 需要root权限才能修改。 实现步骤1)打开rc.local脚本 sudo vi /etc/rc.local不熟悉vi编辑工具的朋友可以使用vim、gedit等工具代替vi 2)在rc.local脚本添加命令在exit 0前添加要执行的命令,里面可以直接写命令或者执行Shell脚本文件sh例如:让ubuntu系统实现每隔5s执行一次温度检测脚本:net-temperature.sh rc.local脚本的设置开机自动执行的sudo命令也是可以执行的;比如: #!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing.: sudo sh run_name.sh exit 0 rc.local命令不执行,程序不启动的问题1、添加log,查看程序执行情况2、rc.local文件头部/bin/sh修改为/bin/bash3、如果是执行sh文件,那么要赋予执行权限sudo chmod +x xxx.sh,然后启动时加上sudo sh xxx.sh 希望对你有帮助。如果有不对的,欢迎指正。 |
CopyRight 2018-2019 实验室设备网 版权所有 |